package com.edu.zju.scs.panacea.service.plan;

import com.edu.zju.scs.panacea.entity.BaseResult;
import com.edu.zju.scs.panacea.entity.plan.Material;
import com.edu.zju.scs.panacea.entity.plan.Plan;
import com.edu.zju.scs.panacea.entity.query.MaterialQuery;
import com.edu.zju.scs.panacea.entity.query.PlanQuery;
import com.edu.zju.scs.panacea.entity.vo.MaterialCheckVO;
import com.edu.zju.scs.panacea.mapper.plan.MaterialMapper;
import com.edu.zju.scs.panacea.mapper.plan.PlanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: Yue Liang
 * @Date: 2019-01-14 14:12
 */
@Service
public class MaterialService {

    @Autowired
    private MaterialMapper materialMapper;

    @Autowired
    private PlanService planService;

    @Autowired
    private PlanMapper planMapper;

    public BaseResult getMaterialCheckBySite(Long siteId) {
        BaseResult baseResult = new BaseResult();
        List<MaterialCheckVO> materialCheckVOList = materialMapper.selectMaterialToCheck(siteId);
        if (materialCheckVOList == null) {
            baseResult.error("该站点对应的素材");
        } else {
            baseResult.success(materialCheckVOList);
        }
        return baseResult;
    }

    /**
     * 根据ID查询某个素材信息
     * @param id
     * @return
     */
    public BaseResult getMaterial(Long id) {
        BaseResult baseResult = new BaseResult();
        Material material = materialMapper.selectMaterial(id);
        if (material == null) {
            baseResult.error("无此id对应的素材");
        } else {
            baseResult.success(material);
        }
        return baseResult;
    }


    /**
     *
     * @param id
     * @param type 0表示通过 1表示拒绝
     * @return
     */
    public BaseResult auditMaterial(Long id, Integer type, String remark) {
        BaseResult baseResult = new BaseResult();
        if (type == 0) {
            materialMapper.passMaterial(id);
        }
        if (type == 1) {
            materialMapper.rejectMaterial(id, remark);
        }
        planService.auditPlan(id);
        baseResult.success();
        return baseResult;
    }

    /**
     * 根据资源位ID找到资源位对应的素材列表
     * @param locId
     * @return
     */
    public BaseResult launchMaterial(Long locId) {
        BaseResult baseResult = new BaseResult();

        // 找到该资源位应于今天投放的所有计划
        PlanQuery planQuery = new PlanQuery();
        planQuery.setLocId(locId);
        planQuery.setDate(new Date());
        List<Plan> planList = planMapper.selectSpecificPlans(planQuery);

        List<Material> materialResult = new ArrayList<>();
        // 从中找到接下来一小时的所有计划
        for (Plan plan : planList) {
            int planLaunch = Integer.parseInt(plan.getBinLaunchTime(), 2);
            int nextLaunch = Integer.parseInt(getNextBinString(), 2);
            if ((planLaunch & nextLaunch) > 0) {
                MaterialQuery materialQuery = new MaterialQuery();
                materialQuery.setPlanId(plan.getId());
                materialQuery.setLocId(locId);
                List<Material> materials = materialMapper.selectSpecificMaterials(materialQuery);
                materialResult.addAll(materials);
            }
        }
        baseResult.success(materialResult);
        return baseResult;
    }

    private String getNextBinString() {
        Date date = new Date(System.currentTimeMillis());
        int hour = date.getHours();
        System.out.println("hours:"+hour);
            String launchTime = "";
            for (int i = 0; i < 24; i++) {
                if (i == hour) {
                    launchTime += "1";
                } else {
                    launchTime += "0";
                }
            }
            return launchTime;
    }

}
